package com.springboot.activiti.config.activiti;

import java.util.ArrayList;
import java.util.List;

import org.activiti.engine.identity.Group;
import org.activiti.engine.impl.GroupQueryImpl;
import org.activiti.engine.impl.Page;
import org.activiti.engine.impl.persistence.entity.GroupEntity;
import org.activiti.engine.impl.persistence.entity.GroupEntityManager;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import com.springboot.activiti.model.GroupModel;
import com.springboot.activiti.service.GroupService;

@Component
public class CustomGroupEntityManager extends GroupEntityManager {

	private Logger logger = LoggerFactory.getLogger(this.getClass());

	@Autowired
	private GroupService groupService;
	
	@Override
	public List<Group> findGroupByQueryCriteria(GroupQueryImpl query, Page page) {
		logger.debug("查询自定义群组："+query+" page:"+page);
		return super.findGroupByQueryCriteria(query, page);
	}

	@Override
	public long findGroupCountByQueryCriteria(GroupQueryImpl query) {
		logger.debug("查询自定义群组数量："+query);
		return super.findGroupCountByQueryCriteria(query);
	}

	@Override
	public List<Group> findGroupsByUser(String userId) {
		logger.debug("查询自定义群组，使用userId："+userId);
		
		List<GroupModel> groupList = groupService.selectList("findGroupsByUser", userId);
		List<Group> groups = new ArrayList<Group>();
		for(GroupModel group : groupList){
			GroupEntity g = new GroupEntity();
			g.setId(group.getGroupId());
			g.setName(group.getGroupName());
			g.setType(group.getGroupType());
			groups.add(g);
		}
		
		return groups;
	}

	
	@Override
	public void insertGroup(Group group) {
		logger.debug("不允许在流程中新增群组");
		throw new UnsupportedOperationException();
	}

	@Override
	public void updateGroup(Group updatedGroup) {
		logger.debug("不允许在流程中修改群组");
		throw new UnsupportedOperationException();
	}

	@Override
	public Group createNewGroup(String groupId) {
		logger.debug("不允许在流程中创建群组");
		throw new UnsupportedOperationException();
	}

	@Override
	public void deleteGroup(String groupId) {
		logger.debug("不允许在流程中删除群组");
		throw new UnsupportedOperationException();
	}

}
